package com.sugon.modules.device.model.dto;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.modules.device.model.DevMaintenPlanItemVO;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ： YuXD
 * @description： 每周起止项ID
 * @date ： 2020-10-28 下午 5:10
 * @version: 1.0
 */
@Data
public class WeekRangeItemDTO implements Serializable {

    /**
     * 工时累计
     */
    private Long manHoursTotal;

    /**
     * 周次
     */
    private Integer weeklyOfYear;

    /**
     * t.value + 第多少周
     */
    private String weekOfYear;

    /**
     * 当前周的开始
     */
    private String beginOfWeek;

    /**
     * 每周的第一天日期
     */
    private Date beginOfWeekDate;

    /**
     * 当前周的结束
     */
    private String endOfWeek;

    /**
     * 每周的最后一天日期
     */
    private Date endOfWeekDate;

    /**
     * 本周的天数
     */
    private Long days;

    /**
     * 设备维护计划项管理
     * KEY: 设备ID
     * VALUE: 设备维护项
     */
    private Map<String, List<DevMaintenPlanItemVO>> planItemMap;

    public WeekRangeItemDTO() {
        super();
        this.manHoursTotal = 0L;
        this.planItemMap = MapUtil.newHashMap();
    }

    /**
     * 累加工时
     *
     * @param manHours
     */
    public void addManHours(Long manHours) {
        if (manHours == null) {
            manHours = 0L;
        }
        this.manHoursTotal += manHours;
    }

    public void addDevPlanItemMap(String devId, List<DevMaintenPlanItemVO> newPlanItems) {
        if (!this.planItemMap.containsKey(devId)) {
            this.planItemMap.put(devId, CollectionUtil.newArrayList());
        }
        this.planItemMap.get(devId).addAll(newPlanItems);
        // 累加工时
        this.manHoursTotal += newPlanItems.stream().mapToLong(item -> item.getCheckDatePrud() + item.getCheckDateUprud()).sum();
    }

    public Long getOriginManHoursTotal() {
        return manHoursTotal;
    }

    public String getManHoursTotal() {
        return manHoursTotal == 0 ? "" : manHoursTotal + "";
    }

    public String getWeekOfYearNoT() {
        return weekOfYear.replace("t.", "");
    }

}
